using System;
using UnityEngine;

public class EternalNode : MonoBehaviour
{
	private static EternalNode m_Instance;

	private float m_TimeDelta;

	private float m_LastTime;

	private float _WatchingAdBtnRiseRecover = -1;
	private float m_WatchingAdBtnRiseRecover
	{
		get
		{
			if (_WatchingAdBtnRiseRecover == -1)
			{
				_WatchingAdBtnRiseRecover = PlayerPrefs.GetFloat("WatchingAdBtn", 1800f);
			}
			return _WatchingAdBtnRiseRecover;
		}
		set
		{
			_WatchingAdBtnRiseRecover = value;
			PlayerPrefs.SetFloat("WatchingAdBtn", _WatchingAdBtnRiseRecover);
			PlayerPrefs.Save();
		}
	}

	private float _PhysicalRecoverTime = -1;
	private float m_PhysicalRecoverTime
	{
		get
		{
			if (_PhysicalRecoverTime == -1)
			{
				_PhysicalRecoverTime = PlayerPrefs.GetFloat("PhysicalTime", 0);
			}
			return _PhysicalRecoverTime;
		}
		set
		{
			_PhysicalRecoverTime = value;
			PlayerPrefs.SetFloat("PhysicalTime", _PhysicalRecoverTime);
			PlayerPrefs.Save();
		}
	}

	private string m_PhysicalRecoverTimeFull;

	private const string m_QuitTimeKey = "QuitTime";

	private const string m_LastPhysicalKey = "LastPhysical";

	private const string m_VedioMissionKey = "VedioMission";

	private const string m_VedioQuickFireKey = "VedioQuickFire";

	private const string m_VedioPhysicalPowerKey = "VedioPhysicalPower";

	private const string m_PhysicalTimeKey = "PhysicalTime";

	private const string m_WatchingAdBtnRiseRecoverKey = "WatchingAdBtn";

	private const string m_ExtraDrawCountKey = "ExtraDrawCount";

	private const string m_LastChipkey = "LastChip";

	private const string m_LastDrawVideoTimeKey = "LastDrawVideoTime";

	private const string m_FirstOpenTime = "FirstOpenTime";

	public const string RATE_KEY = "StarRateKey";

	private DateTime m_PrevDateTime;

	private long _lastDrawVideoTime = -1;

	private bool _bLastClickClose;

	public bool bShowPropTip = true;

	public static EternalNode Instance => m_Instance;

	public float TimeDelta => m_TimeDelta;

	public string watchingAdBtnRiseRecover
	{
		get
		{
			TimeSpan timeSpan = TimeSpan.FromSeconds(1800f - m_WatchingAdBtnRiseRecover);
			string str = (timeSpan.Minutes >= 10) ? timeSpan.Minutes.ToString() : ("0" + timeSpan.Minutes);
			string str2 = (timeSpan.Seconds >= 10) ? timeSpan.Seconds.ToString() : ("0" + timeSpan.Seconds);
			return str + ":" + str2;
		}
	}

	public bool adPlayReady => m_WatchingAdBtnRiseRecover >= 1800f;

	public string physicalRecoverTime
	{
		get
		{
			TimeSpan timeSpan = TimeSpan.FromSeconds(600f - m_PhysicalRecoverTime);
			string str = (timeSpan.Minutes >= 10) ? timeSpan.Minutes.ToString() : ("0" + timeSpan.Minutes);
			string str2 = (timeSpan.Seconds >= 10) ? timeSpan.Seconds.ToString() : ("0" + timeSpan.Seconds);
			return str + ":" + str2;
		}
	}

	public string physicalRecoverTimeFull
	{
		get
		{
			if (m_PhysicalRecoverTimeFull == null)
			{
				TimeSpan timeSpan = TimeSpan.FromSeconds(600.0);
				string str = (timeSpan.Minutes >= 10) ? timeSpan.Minutes.ToString() : ("0" + timeSpan.Minutes);
				string str2 = (timeSpan.Seconds >= 10) ? timeSpan.Seconds.ToString() : ("0" + timeSpan.Seconds);
				m_PhysicalRecoverTimeFull = str + ":" + str2;
			}
			return m_PhysicalRecoverTimeFull;
		}
	}

	public bool isFirstLanding => !PlayerPrefs.HasKey("FirstOpenTime");

	public long lastDrawVideoTime
	{
		get
		{
			if (_lastDrawVideoTime == -1)
			{
				try
				{
					_lastDrawVideoTime = long.Parse(PlayerPrefs.GetString("LastDrawVideoTime", ""));
				}
				catch (Exception e)
				{
					_lastDrawVideoTime = 0;
				}
			}
			return _lastDrawVideoTime;
		}
		set
		{
			_lastDrawVideoTime = value;
			PlayerPrefs.SetString("LastDrawVideoTime", _lastDrawVideoTime.ToString());
			PlayerPrefs.Save();
		}
	}

	public bool bLastClickClose
	{
		get
		{
			return _bLastClickClose;
		}
		set
		{
			_bLastClickClose = value;
		}
	}

	private void Awake()
	{
		if ((bool)m_Instance)
		{
			UnityEngine.Object.Destroy(base.gameObject);
			return;
		}
		m_Instance = this;
		UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
		ResetData();
	}

	private void Start()
	{
		AdManagerOld.instance.curAd.SetNotificationText(HOLocalizationConfiguration.GetValue(100), HOLocalizationConfiguration.GetValue(423), HOLocalizationConfiguration.GetValue(424));
	}

	private void Update()
	{
		float realtimeSinceStartup = Time.realtimeSinceStartup;
		m_TimeDelta = realtimeSinceStartup - m_LastTime;
		m_LastTime = realtimeSinceStartup;
		PhysicalRecover();
		CheckOneDayPass();
		CheckAdButtonRecover();
	}

	private void CheckAdButtonRecover()
	{
		if (m_WatchingAdBtnRiseRecover < 1800f)
		{
			m_WatchingAdBtnRiseRecover += Time.deltaTime;
			if (m_WatchingAdBtnRiseRecover > 1800f)
			{
				m_WatchingAdBtnRiseRecover = 1800f;
			}
		}
	}

	public void StartAdBtnRecover()
	{
		m_WatchingAdBtnRiseRecover = 0f;
	}

	private void PhysicalRecover()
	{
		if (ConstantData.physical < 20)
		{
			m_PhysicalRecoverTime += m_TimeDelta;
			if (m_PhysicalRecoverTime > 600f)
			{
				ConstantData.physical++;
				m_PhysicalRecoverTime = 0f;
			}
		}
	}

	private void CheckOneDayPass()
	{
		if (Time.frameCount % 1000 == 0)
		{
			if (DateTime.Now.Day != m_PrevDateTime.Day)
			{
				ConstantData.vedioPhysicalPowerCount = 10;
				ConstantData.vedioQuickFireCount = 3;
				ConstantData.vedioMissionCount = 5;
			}
			m_PrevDateTime = DateTime.Now;
		}
	}

	private void Quit()
	{
		PlayerPrefs.SetString("QuitTime", DateTime.Now.ToString());
		//PlayerPrefs.SetFloat("PhysicalTime", m_PhysicalRecoverTime);
		//PlayerPrefs.SetInt("ExtraDrawCount", ConstantData.extraDrawCount);
		//PlayerPrefs.SetInt("LastPhysical", ConstantData.physical);
		//PlayerPrefs.SetInt("VedioMission", ConstantData.vedioMissionCount);
		//PlayerPrefs.SetInt("VedioQuickFire", ConstantData.vedioQuickFireCount);
		//PlayerPrefs.SetInt("VedioPhysicalPower", ConstantData.vedioPhysicalPowerCount);
		//PlayerPrefs.SetFloat("WatchingAdBtn", m_WatchingAdBtnRiseRecover);
		//PlayerPrefs.SetInt("LastChip", ConstantData.chips);
		//PlayerPrefs.SetString("LastDrawVideoTime", lastDrawVideoTime.ToString());
		if (!PlayerPrefs.HasKey("FirstOpenTime"))
		{
			PlayerPrefs.SetString("FirstOpenTime", DateTime.Now.ToString());
		}
		//ItemMgr.instance.Save();
		RankMgr.Save();
	}

	private void OnDestroy()
	{
		PlayerPrefs.SetString("QuitTime", DateTime.Now.ToString());
		RankMgr.Save();
	}

	public bool IsTheSameDay()
	{
		if (PlayerPrefs.HasKey("FirstOpenTime"))
		{
			string @string = PlayerPrefs.GetString("FirstOpenTime");
			DateTime d = DateTime.Parse(@string);
			DateTime now = DateTime.Now;
			return (now - d).Days == 0;
		}
		return true;
	}

	private void ResetData()
	{
		RankMgr.Load();
		if (PlayerPrefs.HasKey("ExtraDrawCount"))
		{
			ConstantData.extraDrawCount = PlayerPrefs.GetInt("ExtraDrawCount");
		}
		if (PlayerPrefs.HasKey("LastPhysical"))
		{
			ConstantData.physical = PlayerPrefs.GetInt("LastPhysical");
		}
		if (PlayerPrefs.HasKey("LastChip"))
		{
			ConstantData.chips = PlayerPrefs.GetInt("LastChip");
		}
		if (PlayerPrefs.HasKey("VedioMission"))
		{
			ConstantData.vedioMissionCount = PlayerPrefs.GetInt("VedioMission");
		}
		else
		{
			ConstantData.vedioMissionCount = 5;
		}
		if (PlayerPrefs.HasKey("VedioPhysicalPower"))
		{
			ConstantData.vedioPhysicalPowerCount = PlayerPrefs.GetInt("VedioPhysicalPower");
		}
		else
		{
			ConstantData.vedioPhysicalPowerCount = 10;
		}
		if (PlayerPrefs.HasKey("VedioQuickFire"))
		{
			ConstantData.vedioQuickFireCount = PlayerPrefs.GetInt("VedioQuickFire");
		}
		else
		{
			ConstantData.vedioQuickFireCount = 3;
		}
		if (PlayerPrefs.HasKey("PhysicalTime"))
		{
			m_PhysicalRecoverTime = PlayerPrefs.GetFloat("PhysicalTime");
		}
		if (PlayerPrefs.HasKey("WatchingAdBtn"))
		{
			m_WatchingAdBtnRiseRecover = PlayerPrefs.GetFloat("WatchingAdBtn");
		}
		if (PlayerPrefs.HasKey("LastDrawVideoTime"))
		{
			try
			{
				lastDrawVideoTime = long.Parse(PlayerPrefs.GetString("LastDrawVideoTime"));
			}
			catch (Exception e)
			{

			}
		}
		if (PlayerPrefs.HasKey("QuitTime"))
		{
			DateTime d = DateTime.Parse(PlayerPrefs.GetString("QuitTime"));
			DateTime now = DateTime.Now;
			int num = (int)(now - d).TotalSeconds;
			int num2 = Mathf.Max(0, num / 600);
			if (ConstantData.physical < 20)
			{
				ConstantData.physical = Mathf.Min(ConstantData.physical + num2, 20);
			}
			if (ConstantData.physical >= 20)
			{
				m_PhysicalRecoverTime = 0f;
			}
			else
			{
				int num3 = num % 600;
				m_PhysicalRecoverTime = (m_PhysicalRecoverTime + (float)num3) % 600f;
				m_PhysicalRecoverTime = Mathf.Clamp(m_PhysicalRecoverTime, 0f, 600f);
			}
			m_WatchingAdBtnRiseRecover += num;
			m_WatchingAdBtnRiseRecover = Mathf.Clamp(m_WatchingAdBtnRiseRecover, 0f, 1800f);
			if (d.Day != now.Day)
			{
				ConstantData.vedioPhysicalPowerCount = 10;
				ConstantData.vedioQuickFireCount = 3;
				ConstantData.vedioMissionCount = 5;
			}
		}
		m_PrevDateTime = DateTime.Now;
	}

	private void OnApplicationQuit()
	{
		Quit();
	}

	private void OnApplicationPause(bool pauseState)
	{
		if (pauseState)
		{
			Quit();
		}
		else
		{
			ResetData();
		}
	}

	private void VedioPlayedCallback_Battle()
	{
		GameManager.Rules.Continue();
	}

	private void VedioPlayedCallback_Launch()
	{
		Instance.StartAdBtnRecover();
		UIADViewWnd.Show(UIADViewWnd.ADViewType.Get, CommonConfig.CommonCfgInfo.videoPowerReward);
		ConstantData.physical += CommonConfig.CommonCfgInfo.videoPowerReward;
	}

	private void VedioPlayedCallback_Draw()
	{
		_lastDrawVideoTime = DateTime.Now.ToFileTime();
		ConstantData.extraDrawCount += CommonConfig.CommonCfgInfo.addDrawCountVal;
		LaunchUIController.instance.luckyPanel.GetComponentInChildren<UILucky>().StartTurn();
	}

	public TimeSpan GetDrawDateTimeSpan()
	{
		DateTime d = DateTime.FromFileTime(Instance.lastDrawVideoTime).AddSeconds(CommonConfig.CommonCfgInfo.addDrawCountSpan + 1);
		return d - DateTime.Now;
	}

	private void VedioPlayedCallback_GetChips()
	{
		Instance.StartAdBtnRecover();
		ConstantData.chips += CommonConfig.CommonCfgInfo.videoChipReward;
		UIADViewWnd.Show(UIADViewWnd.ADViewType.Get, 0, CommonConfig.CommonCfgInfo.videoChipReward);
	}

	private void VedioPlayedCallback_Gift()
	{
		string name = string.Empty;
		int type = -1;
		if (GameManager.GType == GameType.Mission)
		{
			type = 0;
			LevelData currentLevel = LevelDataIndex.CurrentLevel;
			name = "-1";
		}
		else if (GameManager.GType == GameType.AI)
		{
			type = 1;
			name = AIPlayer.curBattleInfo.id + string.Empty;
		}
		else if (GameManager.GType == GameType.QuickFire)
		{
			type = 1;
			name = "1";
		}
		LevelGift leveGift = GiftMgr.GetLevel(type, name);
		if (leveGift != null)
		{
			UIADViewWnd.ShowGetItem(leveGift.itemId, leveGift.itemCount, delegate
			{
				ItemMgr.instance.AddItem(leveGift.itemId, leveGift.itemCount);
			}, autoTrigger: true);
			return;
		}
		ProbInfo probInfo = GiftMgr.GetItem();
		if (probInfo != null)
		{
			UIADViewWnd.ShowGetItem(probInfo.itemId, probInfo.count, delegate
			{
				ItemMgr.instance.AddItem(probInfo.itemId, probInfo.count);
			}, autoTrigger: true);
		}
	}
}
